//电阻实现的旋钮开关
//11档旋钮开关
//档位之间接12k电阻

#include "APP_Main.h"

uint16_t switch_tick200ms;
uint16_t Switch_Show_Count;
uint16_t Switch_State = Temp_Shutoff;
uint8_t switch_trigger_Flag = 0;
uint16_t sample_value = 0; //缓冲100ms,避免返回局部变量值0


void Switch_IOConfig(void)
{
	/*
    GPIO_InitSettingType y;

    y.Signal = GPIO_Pin_Signal_Analog;
    y.Dir = GPIO_Direction_Input;
    y.Func = GPIO_Reuse_Func0;
    y.ODE = GPIO_ODE_Output_Disable;
    y.DS = GPIO_DS_Output_Normal;
    y.PUE = GPIO_PUE_Input_Disable;
    y.PDE = GPIO_PDE_Input_Disable;
    GPIO_Init(IO_switch, &y);
	 * */

    GPIO_Init(GPIOA0,IO_Switch,Intput);
}

//tick 2ms
void switch_clock(void)
{
    if(switch_tick200ms < 0xffff) switch_tick200ms++;
}

void Switch_Trigger(void)
{
    if(FactoryEnable == 0)
    { //非出厂测试,闪耀有效
        if(Switch_State != Temp_Set_Smoke)
        {
            NT_Shine_Rest();
            switch_trigger_Flag = 1;
        }
    }
    ADC_KeyUpDown();
}

void SwitchKey(void)
{
    if(switch_tick200ms >= 100)
    {
        switch_tick200ms -= 100;
        sample_value = SamplingSingle(ADC_Switch); //测量的AD范围值+-20作为判断范围
    }

    if(sample_value >= 982)
    { //1023 [sample_value * 0.04, ]
        if(Switch_State != Temp_Set_Smoke)
        {
            Switch_State = Temp_Set_Smoke; //Smoke //AD: 4095-4095
            Switch_Trigger();
            NT_Shine_Stop();
        } 
    }
    else if((sample_value >= 807) && (sample_value <= 967))
    { //841 [sample_value * 0.04, sample_value * 0.15]
        if(Switch_State != Temp_Set_200)
        {
            Switch_State = Temp_Set_200; //AD: [3353 - 50,3354 + 50], 220v供电: [3457-50,3460+50]
            Switch_Trigger();
        }
    }
    else if((sample_value >= 703) && (sample_value <= 777))
    { //740 [sample_value * 0.05, sample_value * 0.05]
        if(Switch_State != Temp_Set_225)
        {
            Switch_State = Temp_Set_225; //AD: [2979 - 50,2980 + 50], 220v供电: [3074-50,3076+50]`
            Switch_Trigger();
        }
    }
    else if((sample_value >= 607) && (sample_value <= 684))
    { //646 [sample_value * 0.06, sample_value * 0.06]
        if(Switch_State != Temp_Set_250)
        {
            Switch_State = Temp_Set_250; //AD: [2607 - 50,2609 + 50], 220v供电: [2687-50,2691+50]
            Switch_Trigger();
        }
    }
    else if((sample_value >= 521) && (sample_value <= 599))
    { //555 [sample_value * 0.06, sample_value * 0.08]
        if(Switch_State != Temp_Set_300)
        {
            Switch_State = Temp_Set_300; //AD: [2234 - 50,2236 + 50], 220v供电: [2304-50,2307+50]
            Switch_Trigger();
        }
    }
    else if((sample_value >= 419) && (sample_value <= 498))
    { //466 [sample_value * 0.1, sample_value * 0.07]
        if(Switch_State != Temp_Set_350)
        {
            Switch_State = Temp_Set_350; //AD: [1859 - 50,1861 + 50], 220v供电: [1916-50,1918+50]`
            Switch_Trigger();
        }
    }
    else if((sample_value >= 322) && (sample_value <= 397))
    { //379 [sample_value * 0.15, sample_value * 0.05]
        if(Switch_State != Temp_Set_400)
        {
            Switch_State = Temp_Set_400; //AD: [1486 - 50,1487 + 50], 220v供电: [1529-50,1537+50]
            Switch_Trigger();
        } 
    }
    else if((sample_value >= 232) && (sample_value <= 333))
    { //290 [sample_value * 0.2, sample_value * 0.15]
        if(Switch_State != Temp_Set_450)
        {
            Switch_State = Temp_Set_450; //AD: [1113 - 50,1114 + 50], 220v供电: [1147-50,1149+50]
            Switch_Trigger();
        }
    }
    else if((sample_value >= 158) && (sample_value <= 227))
    { //198 [sample_value * 0.2, sample_value * 0.15]
        if(Switch_State != Temp_Set_475)
        {
            Switch_State = Temp_Set_475; //AD: [739 - 50,740 + 50], 220v供电: [759-50,763+50]
            Switch_Trigger();
        }
    }
    else if((sample_value >= 75) && (sample_value <= 140))
    { //100; [sample_value * 0.25, sample_value * 0.4]
        if(Switch_State != Temp_Set_500)
        {
            Switch_State = Temp_Set_500; //AD: [366 - 50,367 + 50], 220v供电: [366-50,379+50]
            Switch_Trigger();
        }  
    }
    else if((sample_value >= 0) && (sample_value < 50))
    { //0  ShutDown
        Switch_State = Temp_Shutoff; //AD: [0,191 + 50], 上电后一段时间采集的ad值是191
        ADC_KeyUpDown();
    }

    temp_set = Switch_State; //设置FanCtrl的温度设定值
}
